package com.example.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class BatUtils{
    private  static  BatUtils dataBase;
    private  BatUtils(){}
    public  static  BatUtils instance(){//是否可用单利来管理
        if (dataBase == null){
            System.out.println("BatUtils.session");
            dataBase = new BatUtils();
        }
        return dataBase;
    }
    private  static SqlSessionFactory sqlSessionFactory;
    public static SqlSession session() throws Exception{
        return session(ExecutorType.SIMPLE,false);
    }
    public static SqlSession session(ExecutorType var1, boolean var2) throws Exception{
        try {
            String config = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(config);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = builder.build(inputStream);
        }catch (IOException error){
            throw new IOException(error);
        }
        SqlSession session = sqlSessionFactory.openSession(var1,var2);
        BatUtils utils = BatUtils.instance();
        System.out.println("BatUtils address "+utils);
        System.out.println("SqlSession address "+session);
        return session;
    }
}
